package com.cn.util;

import com.asprise.ocr.Ocr;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @program: demo
 * @description:
 * @author: yihanyuan
 * @create: 2019-07-16 15:52
 */
public class Ocr1 {
    public static void main(String[] args) throws IOException, IOException {
        HttpClient httpClient = new HttpClient();
//        GetMethod getMethod = new GetMethod("https://m.client.10010.com/sma-lottery/qpactivity/getSysManageLoginCode.htm?userid=E871D913EAFBB20446AB358699291689&code=1563261797816");
        GetMethod getMethod = new GetMethod("https://dynamic.12306.cn/otsweb/passCodeAction.do?rand=sjrand");
        int statusCode = httpClient.executeMethod(getMethod);
        if (statusCode != HttpStatus.SC_OK) {
            System.err.println("Method failed: " + getMethod.getStatusLine());
            return;
        }
        File filepicF = File.createTempFile("temp", ".jpg");//创建临时文件
        InputStream inputStream = getMethod.getResponseBodyAsStream();
        OutputStream outStream = new FileOutputStream(filepicF);
        IOUtils.copy(inputStream, outStream);
        outStream.close();
        Ocr.setUp(); // one time setup
        Ocr ocr = new Ocr(); // create a new OCR engine
        ocr.startEngine("eng", Ocr.SPEED_FASTEST); // English
        String s = ocr.recognize(new File[]{filepicF}, Ocr.RECOGNIZE_TYPE_TEXT, Ocr.OUTPUT_FORMAT_PLAINTEXT);
        System.out.println("Result: " + s);

        System.out.println("图片文字为:" + s.replace(",", "").replace("i", "1").replace(" ", "").replace("'", "").replace("o", "0").replace("O", "0").replace("g", "6").replace("B", "8").replace("s", "5").replace("z", "2"));
// ocr more images here ...
        ocr.stopEngine();
    }

    public static File downPic() throws IOException {
        String dirc = "E:\\pic";
        URL url = null;
        String picName = "1.png";
        DataInputStream dataInputStream = null;
        File file = null;
        FileOutputStream fileOutputStream = null;
        try {
            file = new File(dirc);
            if (!file.exists()) {//如果文件夹不存在
                file.mkdir();//创建文件夹
            }
            url = new URL("https://m.client.10010.com/sma-lottery/qpactivity/getSysManageLoginCode.htm?userid=E871D913EAFBB20446AB358699291689&code=1563261797816");
            dataInputStream = new DataInputStream(url.openStream());
            fileOutputStream = new FileOutputStream(new File(dirc + "\\" + picName));
            byte[] bytes = new byte[1024 * 1024];
            int length;
            while ((length = dataInputStream.read(bytes)) > 0) {
                fileOutputStream.write(bytes, 0, length);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                dataInputStream.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return new File(dirc + "\\" + picName);
    }
}
